<?php
/**
 * 预立项申请
 */
require_once("../global.inc.php");

class SysProjectOfficialClass extends SystemFrameWork{
	/**
	 * 预立项申请对象
	 *
	 * @var obj
	 */
	var $obj_project;
	/**
	 * 审批对象
	 *
	 * @var obj
	 */
	var $obj_project_verify;
	/*
	 *公司
	 */
	var $obj_system_power;
	var $obj_project_member;
	var $obj_department;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	
	var $level = array('一级', '二级', '三级', '四级', '五级', '六级', '七级', '八级', '九级', '十级');
	
	function main(){
		/**
		 * 员工
		 */
		if (!is_object($this->obj_system_power)){
			require_once ("system_power.class.php");
			$this->obj_system_power = new SystemPowerClass();
		}
		/**
		 * 项目
		 */
		if (!is_object($this->obj_project)){
			require_once ("project.class.php");
			$this->obj_project = new ProjectClass();
		}
		if (!is_object($this->obj_department)){
			require_once('department.class.php');
			$this->obj_department = new DepartmentClass();
		}
		if (!is_object($this->obj_project_member)){
			require_once ("project_member.class.php");
			$this->obj_project_member = new ProjectMemberClass();
		}
		/**
		 * 项目审批
		 */
		if (!is_object($this->obj_project_verify)){
			require_once ("project_verify.class.php");
			$this->obj_project_verify = new ProjectVerifyClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 初始化分页类
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}
		
		switch($this->_input['action']){
			case "ajax_list":
				$this->_ajax_list();
				break;
			case "mylist":
				$this->_mylist();
				break;
			case "add":
				$this->_add();
				break;
			case "save":
				$this->_save();
				break;
			case "modi":
				$this->_modi();
				break;
			case "update":
				$this->_update();
				break;
			case 'view':
				$this->_view();
				break;
			case "del":
				$this->_del();
				break;
			case "liucheng":
				$this->_liucheng();
				break;
			case "gaojian":
				$this->_gaojian();
				break;
			case "chengyuan":
				$this->_chengyuan();
				break;
			case "huodong":
				$this->_huodong();
				break;
			case "cehua":
				$this->_cehua();
				break;
			case "verify":
				$this->_verify();
				break;
			case "doverify":
				$this->_doverify();
				break;
			case "get_module":
				$this->_get_module();
				break;
			default:
				$this->_list();
		}
	}
	/**
	 * 列表
	 */
	function _list(){
		$this->_ajax_list();
	}
	
	/**
	 * ajax类表
	 */
	function _ajax_list(){
		//搜索条件
		$condition = 'where project.project_step in (2,3,4,5,6) and project.status=1 order by project.id desc';
		
		//信息列表
		$this->obj_page->pagebarnum(10);
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		$project_array = $this->obj_project->listProject($condition,$this->obj_page);
		$page_list = $this->obj_page->show(2);
		
		if (is_array($project_array)){
			//审批流程
			$flow_array = include('flow_config.ini.php');
			for ($i=0;$i<count($project_array);$i++){
				//对时间进行格式化
				$project_array[$i]['draft_date'] = @date("Y-m-d",$project_array[$i]['draft_date']);
				
				if ($project_array[$i]['nowflow_id']) {
					//获取审批明细
					$verify_array = $this->obj_project_verify->getProjectVerify($project_array[$i]['nowflow_id']);
					$flow_name = $flow_array[$verify_array['verify_flow']]['name'];
					if ($verify_array['verify_status'] == 1) {
						$verify_status = '审批通过';
					}elseif ($verify_array['verify_status'] == 2){
						$verify_status = '<font color=red>审批打回</font>';
					}else{
						$verify_status = '待审批';
					}
				}else{
					if ($project_array[$i]['is_submit'] == 0) {
						$flow_name = '无';
						$verify_status = '未提交';
					}else {
						$flow_name = '无';
						$verify_status = '已提交';
					}
				}
				$project_array[$i]['flow_name'] = $flow_name;
				$project_array[$i]['verify_status'] = $verify_status;
			}
		}
		/**
		 * 页面输出
		 */
		$this->output("user_id", $_SESSION['s_admin']['id']);
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output('project_array',$project_array);
		$this->output('page_list',$page_list);
		$this->output('group_name',$_SESSION['s_admin']['group_name']); //角色
		$this->output('condition',$this->_input);
		$this->showpage('sys_project_official.list');
	}
	/**
	 * 列表
	 */
	function _mylist(){
		$this->showpage('sys_project_official.mylist');
	}
	
	/**
	 * 信息添加
	 */
	function _add(){
		
		$this->showpage('sys_project_official.add');
	}
	/**
	 * 信息添加
	 */
	function _save(){
		$this->redirectPath('error','./project.manage.php?action=list','创建预立项申请成功！',1);
			
	}
	
	/**
	 * 修改信息
	 */
	function _modi(){
		$this->showpage('sys_project_official.modi');
	}
	
	/**
	 * 保存修改
	 */
	function _update(){
		$this->redirectPath('error','./project.manage.php?action=list','更新预立项申请成功！',1);
			
	}
	/**
	 * 查看
	 */
	function _view(){
		$this->output('step',1);
		$this->showpage('sys_project_official.view');
	}
	/**
	 * 删除信息
	 */
	function _del(){
		$this->redirectPath('error', '' , '删除预立项申请成功！', 1);

	}
	function _liucheng(){
		$this->showpage('sys_project_official.liucheng');
	}
	
	function _gaojian(){
		$this->showpage('sys_project_official.gaojian');
	}
	
	function _chengyuan(){
		$this->showpage('sys_project_official.chengyuan');
	}
	
	function _huodong(){
		$this->showpage('sys_project_official.huodong');
	}
	
	function _cehua(){
		$this->showpage('sys_project_official.cehua');
	}
	public function _verify(){
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$project_array = $this->obj_project->getProject($this->_input["id"], 'more');
			
			//获取提交人
			$member_array = $this->obj_system_power->getSysMemberById($project_array['user_id']);
			$project_array['user_name'] = $member_array['real_name'];
				
			//获取部门信息
			$dept_array = $this->obj_department->getDepartmentList('', $obj_page);
			//审批流程
			$flow_array = include('flow_config.ini.php');
			//print_r($project_array);
			if ($project_array['project_step'] == 2) { //分配人员表
				
				$showpage = 'viewfenpei';
				
				//获取分配人员信息
				$where = 'where project_member.project_id='.$this->_input['id'].' and project_member.status=1';
				$member_array = $this->obj_project_member->listProjectMember($where, $obj_page);
				if ($member_array) {
					foreach ($member_array as $k => $member) {
						foreach ($dept_array as $dept){
							if($dept['id'] == $member['dept_id']){
								$member_array[$k]['dept_name'] = $dept['name'];
							}
						}
						//格式化日期
						if ($member['start_date']) {
							$member_array[$k]['start_date'] = date('Y-m-d', $member['start_date']);
						}else{
							$member_array[$k]['start_date'] = date('Y-m-d');
						}
						if ($member['end_date']) {
							$member_array[$k]['end_date'] = date('Y-m-d', $member['end_date']);
						}else{
							$member_array[$k]['end_date'] = date('Y-m-d');
						}
					}
				}
				
			}elseif ($project_array['project_step'] == 3){ //人员成本表
				
				$showpage = 'viewchengben';
				
				//获取分配人员信息
				$where = 'where project_member.project_id='.$this->_input['id'].' and project_member.status=1';
				$member_array = $this->obj_project_member->listProjectMember($where, $obj_page);
				if ($member_array) {
					foreach ($member_array as $k => $member) {
						//工作天数
						$member_array[$k]['days'] = intval(($member['end_date'] - $member['start_date']) / 86400);
						$member_array[$k]['day_gongzi'] = 0;
						if ($member['gongzi'] > 0) {
							$member_array[$k]['day_gongzi'] = intval($member['gongzi']/22);
						}
						$member_array[$k]['total_gongzi'] = $member_array[$k]['days'] * $member_array[$k]['day_gongzi'];
					}
				}
				
			}else {
				
			}
			
			$vid = $project_array['nextflow_id'];
			if ($vid != 0) {
				//根据下一步流程ID获取信息
				$next_array = $this->obj_project_verify->getProjectVerify($vid);
				//获取当前审批阶段
				$project_array['flow_name'] = $flow_array["{$next_array['verify_flow']}"]['name'];
				
				$where = " where project_id='".$project_array['id']."' and verify_flow='".$next_array['verify_flow']."' order by id asc";
				$verify_array = $this->obj_project_verify->listProjectVerify($where, $obj_page);
				if ($verify_array) {
					$i = 0;
					foreach ($verify_array as $key => $val){
						$verify_array[$key]['verify_level'] = $this->level[$i];
						
						//获取角色名
						$group_array = $this->obj_system_power->getSysGroupById($val['verify_role']);
						if ($group_array) {
							$verify_array[$key]['verify_role'] = $group_array['sys_group_name'];
						}
						
						if ($val['member_id']) {
							$members_array = $this->obj_system_power->getSysMemberById($project_array['user_id']);
							$verify_array[$key]['user_name'] = $members_array['real_name'];
						}
						if ($val['verify_time']) {
							$verify_array[$key]['verify_time'] = date('Y-m-d', $val['verify_time']);
						}
						if ($val['verify_status'] == 1) {
							$verify_array[$key]['verify_status'] = '审批通过';
						}elseif ($val['verify_status'] == 2){
							$verify_array[$key]['verify_status'] = '<font color=red>审批打回</font>';
						}else {
							$verify_array[$key]['verify_status'] = '待审批';
						}
						$i++;
					}
				}
			}
				
			$this->output("curpage", $this->_input['curpage']);
			$this->output('member_array',$member_array);
			$this->output('project_array',$project_array);
			$this->output('verify_array',$verify_array);
			$this->output('vid',$vid);	
			$this->showpage('distribute_mem.'.$showpage);
		}
        
    }
    
    function _doverify(){
    	/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			
			$value_array = array();
			$value_array['id'] = $this->_input["id"];
			$value_array['member_id'] = $_SESSION['s_admin']['id'];
			$value_array['verify_time'] = time();
			$value_array['verify_options'] = $this->_input["verify_options"];
			$value_array['verify_status'] = $this->_input["verify_status"];
			$result = $this->obj_project_verify->updateProjectVerify($value_array);
			if ($result != true){
				$this->redirectPath('error','','审批失败！',1);
			}else {
				//更新项目表
				$value_array = array();
				$value_array['id'] = $this->_input["project_id"];
				$value_array['nowflow_id'] = $this->_input["id"];
				
				if ($this->_input["verify_status"] == 2) {//打回
					$value_array['is_submit'] = 0;
					$value_array['nextflow_id'] = 0;
				}else {
					//根据下一步流程ID获取信息
					$now_array = $this->obj_project_verify->getProjectVerify($this->_input["id"]);
					
					//审批流程
					$where = " where project_id='".$this->_input["project_id"]."' and verify_flow='".$now_array['verify_flow']."' order by id asc";
					$verify_array = $this->obj_project_verify->listProjectVerify($where, $obj_page);
					if ($verify_array) {
						for ($i=0; $i<count($verify_array); $i++){
							if ($verify_array[$i]['id'] == $this->_input["id"] && isset($verify_array[$i+1])) {
								$value_array['nextflow_id'] = $verify_array[$i+1]['id'];
								break;
							}
						}
					}
					if (!$value_array['nextflow_id']) {
						$value_array['is_submit'] = 0;
						$value_array['project_step'] = $now_array['verify_flow']+1;
						$value_array['nextflow_id'] = 0;
					}
				}
				$this->obj_project->updateProject($value_array);
				//记录操作日志
				SystemPowerClass::addSysLog('项目->项目审批->项目审批');
				$this->redirectPath('error','./project_official.manage.php?action=list','审批成功！',1);
			}
		}
    }
	/**
	 * 取模块内容
	 */
	function _get_module(){

		$module_name = $this->_input['module_name'];

		switch ($module_name){
			case "selCompany":
				$this->showpage("sys_module_company.info");
				break;
			case 'selCustomer':
				$this->showpage("sys_module_customer.info");
				break;
			case 'selMember':
				$this->showpage("sys_module_member.info");
				break;
			default:
				exit;
		}
	}

}
$project = new SysProjectOfficialClass();
$project->main();
unset($project);
?>